<!--编辑界面上有个列表，然后有个选择按钮-->
<template>
    <div class='input-button-select'>
        
        <el-button type="primary" @click="openSelect">{{label}}</el-button>

        <el-dialog
            title="选择"
            :visible.sync="dialogVisible"
            width="90%"
            append-to-body
           
            >
            <div v-loading="loading">
                <div style="height:70VH">
                    <commonList ref="commonList" @openWin="openWin" :exId="exId"  sKey="" :model="model" :appid="appid"  :model_key="id_" :company="company" searchs="" listnames='' @rowMultiClick="rowMultiClick" @rowClick="rowClick" v-if="isList=='Y'&&dialogVisible"></commonList>
                    
                    <cemetery-mx  ref="commonList"  isSelect="Y1" v-if="componentType=='cemetery'&&dialogVisible" isCheckbox="Y"  isEmpty="Y"></cemetery-mx>
                </div>
                
            
                <div slot="footer" class="dialog-footer" style='text-align:center'>
                    <el-button @click="dialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="selects"   >选 择</el-button>
                    <el-button type="primary" @click="selects('Y')"   >选择后关闭</el-button>
                    <el-button type="primary" @click="selectno('Y')"  v-if="isList=='Y'&&extypes=='simple'" >取消选择</el-button>
                </div>
            </div>
        </el-dialog>
        
    </div>
</template>
<script>
import commonList  from '../list/commonList.vue'
import CemeteryMx  from '../gm/CemeteryMx.vue'
export default {
    name:'input-button-select',
    props:['exkey','extype','id','label','data','appid','dataField','index'],
    components: {
        commonList,
        CemeteryMx
    },
    data(){
        return{
            dialogVisible:false,
            company:localStorage.getItem("company"),
            curRow:null,
            curRows:null,
            dataNew:this.data,
            id_:"",
            model:"",
            isList:"Y",
            extypes:this.extype,
            componentType:"",
            loading:false,
            
        }
    },
    created(){
        if(this.extype!="simple"&&this.extype!="double"){
            let extypes=this.extype.split("#");
            this.componentType=extypes[0];
            this.extypes=extypes[1];
            this.isList="N"
        }else{
            let ids=this.id.split("#");
            this.id_=ids[0];
            this.model=ids.length>1 ? ids[1]:""
        }
    },
    computed:{
        exId:function(){
            let id="";
            console.log("exId=============="+this.exkey.exId)
            if(this.exkey.exId){
                let ids=this.exkey.exId.split(".");
                id=this.data[ids[0]][ids[1]];
            }else{
                try{
                id=this.data.sId;
                if(!id&&this.exkey.sId){
                    let ids=this.exkey.sId.split(".");
                    id=this.data[ids[0]][ids[1]];
                }
                }catch(e){}
            }
             
            return id;
            }
    },
    methods:{
        openSelect(){
            this.dialogVisible=true
            
            this.curRow=null
            this.curRows=null
        },
        goSearchList(){
            this.$refs.commonList.loadData();
        },
        openWin(data){
            this.$emit("openWin",data);
            
        },
        
        rowClick(data){
            this.curRow=JSON.parse(data)
        },
        rowMultiClick(data){
            this.curRows=JSON.parse(data);
        },
        selects(isClose){//选择确定按钮
           
            if(this.isList=="Y"){
                if(this.extypes=="simple"){//只选择一条数据
                    if(!this.curRow){
                        this.$message.error('请选择一条数据！');
                        return;
                    }
                    let exkeys=this.exkey;
                    /*if(exkey_.indexOf("{")==-1){
                        let exkey_="{"+this.exkey+"}";
                        exkeys=JSON.parse(exkey_);
                    }*/
                        
                    console.log(exkeys);
                    
                    let type="";
                    let pre="";
                    if(exkeys["type"]){
                        type=exkeys["type"]
                    }
                    if(exkeys["pre"]){
                        pre=exkeys["pre"]
                    // pre=pre+".";
                    }
                    try{
                         if(this.curRows.row.length>0){
                            this.curRow=this.curRows.row[0]
                        }
                    }catch(e){}
                   
                    
                    for(let key in exkeys){
                        let key2=exkeys[key];
                        console.log("key======="+key+"==="+key2+"=="+pre)
                        /*let data={};
                        data.id=pre+key;
                        data.val=this.curRow[key2];
                        this.$emit("input",JSON.stringify(data));*/
                        if(this.index||this.index=='0'){
                            if(key!="pre"&&key!="type"&&key!="reloadImg"&&key!="id"){
                                this.$set(this.data[pre][this.index],key,this.curRow[key2])
                            
                            }
                        }
                        if(key!="pre"&&key!="type"&&key!="reloadImg"&&key!="id"){
                            this.$set(this.data[pre],key,this.curRow[key2])
                                 console.log("key======="+key+"==="+key2+"=="+pre+"=="+this.curRow[key2])
                           
                        }
                    }
                   // alert(exkeys["reloadImg"]);
                    if(exkeys["reloadImg"]=="Y"){
                        sessionStorage.setItem("reloadImg","Y")
                    }
                    if(type=="all"){//覆盖所有字段

                        for(let key in this.curRow){
                        /* let data={};
                            data.id=pre+key;
                            data.val=this.curRow[key];
                            this.$emit("input",JSON.stringify(data));*/
                            if(this.curRow[key].indexOf("<img")==-1)
                                this.$set(this.data[pre],key,this.curRow[key])
                             if(this.index||this.index=='0'){
                                if(key!="pre"&&key!="type"&&this.curRow[key].indexOf("<img")==-1&&key!="id"){
                                   this.$set(this.data[pre][this.index],key,this.curRow[key])
                                
                                }
                            }
                        }
                    }
                }else{//多条数据
                    let item=this.data[this.exkey];
                    console.log(this.curRows,"this.curRows=========================")
                   
                    if(this.curRows){
                        for(let i=0;i<this.curRows.row.length;i++){
                        // this.data[this.exkey].push(this.curRows[i]);
                          
                            if(this.isJSON(this.exkey)){//如果this.exkey是json格式，主要针对选择的列表和实际赋值的列表字段不对应，需要重新指定
                                let row=this.curRows.row[i];
                                let pre=this.exkey["pre"];
                                let data={};
                                for(let key in this.exkey){
                                    if(key!='pre'){
                                        data[key]=row[this.exkey[key]];
                                    }
                                }
                                //设置默认值
                                try{
                                    let temp={};
                                    if(this.data[pre+"_empty"]){
                                        temp=this.data[pre+"_empty"]
                                        console.log("选择成功--------------")
                                        console.log(temp);
                                        for(let keys in temp){
                                            console.log(keys+"=="+data[keys])
                                            if(!data[keys]||data[keys].indexOf("1900-01-01")!=-1||data[keys]==undefined){
                                                if(temp[keys])
                                                    data[keys]=temp[keys]
                                            }
                                        }
                                    }
                               }catch(e){}

                                this.$set(this.data[pre],this.data[pre].length,data);
                            }else{//选择的列表和实际赋值的列表字段对应，直接赋值
                                let row=this.curRows.row[i];
                                let temp={};
                                 //设置默认值
                                try{
                                    if(this.data[this.exkey+"_empty"]){
                                        temp=this.data[this.exkey+"_empty"]
                                        for(let keys in temp){
                                            if(!row[keys]||row[keys].indexOf("1900-01-01")!=-1||row[keys]==undefined){
                                                 if(temp[keys])
                                                    row[keys]=temp[keys]
                                            }
                                        }
                                    }
                               }catch(e){}
                               
                                 this.$set(this.data[this.exkey],this.data[this.exkey].length,this.curRows.row[i]);
                            }
                             console.log(this.data[this.exkey],"this.data--s-s-s-s")  
                            /*
                            try{
                                this.$parent.$parent.$parent.$parent.$refs.tableEdits[0].addItem(JSON.stringify(this.curRows[i]));
                            }catch(e){
                                try{
                                    this.$parent.$parent.$parent.$refs.tableEdits[0].addItem(JSON.stringify(this.curRows[i]));
                                }catch(e){
                                    this.$parent.$parent.$parent.$parent.$parent.$refs.tableEdits[0].addItem(JSON.stringify(this.curRows[i]));
                                }
                            }*/
                        }
                    }else{
                        this.$message.error('请至少勾选一条数据！');
                        return ;
                    }
                    
                }
                if(isClose=="Y")
                    this.dialogVisible=false;
            }else{
               /* const loading = this.$loading({
                    lock: true,
                    text: 'Loading',
                    spinner: 'el-icon-loading',
                });*/
                this.loading=true;
                let items=this.$refs.commonList.selectok2();
                let exkey=this.exkey;
                let pre=exkey.pre;
                let key=exkey.key;
                let datas=JSON.parse(JSON.stringify(this.data[pre]));
                console.log("items===============================================");
                for(let i=0;i<items.length;i++){
                    try{
                        delete items[i]["menus"]
                    }catch(e){}
                    let id=items[i][key];
                    let isOk=true;
                    for(let j=0;j<this.data[pre].length;j++){
                        let id_=this.data[pre][j][key];
                        if(id==id_){
                            isOk=false;
                        }
                    }
                    let temp={};
                        //设置默认值
                    try{
                        if(this.data[pre+"_empty"]){
                            temp=this.data[pre+"_empty"]
                            for(let keys in temp){
                                if(!items[i][keys]||items[i][keys].indexOf("1900-01-01")!=-1||items[i][keys]==undefined){
                                    if(temp[keys])
                                        items[i][keys]=temp[keys]
                                }
                            }
                        }
                    }catch(e){}
                    console.log("墓位选择完成=="+isOk)
                    console.log(items)
                    if(isOk)
                        datas.push(items[i]);
                        //this.$set(this.data[pre],this.data[pre].length,items[i]);
                    
                }
                this.$set(this.data,pre,datas);
                //loading.close();
                
                if(isClose=="Y")
                    this.dialogVisible=false;
                this.loading=false;
                console.log(this.data)
            }
            
            if(isClose=="Y")
                this.dialogVisible=false;
        },
        selectno(isClose){//选择确定按钮
           
           if(this.isList=="Y"){
               if(this.extypes=="simple"){//只选择一条数据
                  
                   let exkeys=this.exkey;
                   /*if(exkey_.indexOf("{")==-1){
                       let exkey_="{"+this.exkey+"}";
                       exkeys=JSON.parse(exkey_);
                   }*/
                       
                   console.log(exkeys);
                   
                   let type="";
                   let pre="";
                   if(exkeys["type"]){
                       type=exkeys["type"]
                   }
                   if(exkeys["pre"]){
                       pre=exkeys["pre"]
                   // pre=pre+".";
                   }
               
                  
                   
                   for(let key in exkeys){
                       let key2=exkeys[key];
                       console.log("key======="+key+"==="+key2+"=="+pre)
                       /*let data={};
                       data.id=pre+key;
                       data.val=this.curRow[key2];
                       this.$emit("input",JSON.stringify(data));*/
                       if(this.index||this.index=='0'){
                           if(key!="pre"&&key!="type"&&key!="reloadImg"&&key!="id"){
                               this.$set(this.data[pre][this.index],key,"")
                           
                           }
                       }
                       if(key!="pre"&&key!="type"&&key!="reloadImg"&&key!="id"){
                           this.$set(this.data[pre],key,"")
                          
                       }
                   }
                  // alert(exkeys["reloadImg"]);
                   if(exkeys["reloadImg"]=="Y"){
                       sessionStorage.setItem("reloadImg","Y")
                   }
                   if(type=="all"){//覆盖所有字段

                       for(let key in this.curRow){
                       /* let data={};
                           data.id=pre+key;
                           data.val=this.curRow[key];
                           this.$emit("input",JSON.stringify(data));*/
                           if(this.curRow[key].indexOf("<img")==-1)
                               this.$set(this.data[pre],key,"")
                            if(this.index||this.index=='0'){
                               if(key!="pre"&&key!="type"&&this.curRow[key].indexOf("<img")==-1&&key!="id"){
                                  this.$set(this.data[pre][this.index],key,"")
                               
                               }
                           }
                       }
                   }
               }
               if(isClose=="Y")
                   this.dialogVisible=false;
           }
           if(isClose=="Y")
               this.dialogVisible=false;
       },
        isJSON(str) {
            if(typeof str == 'object'  ){
                return true;
            }else{
                return false;
            }
            
        }
            
    }
}
</script>
<style scoped>
    .el-dialog {
        display: flex;
        flex-direction: column;
        margin:0 !important;
        position:absolute;
        top:5%;
        left:5%;
        transform:translate(-90%,-90%);
        /*height:600px;*/
        max-height:calc(100% - 10px);
        max-width:calc(100% - 1px);
    }
    .el-dialog .el-dialog__body {
        flex:1;
        overflow: auto;
    }

    .input-button-select{
        padding-left:5px;
        display:inline-block;
        margin-bottom:5px;
        margin-top:5px;
    }
</style>
<style >
    .el-dialog {
        display: flex;
        flex-direction: column;
        margin:0 !important;
        position:absolute;
        top:5%;
        left:5%;
    }
    .el-dialog .el-dialog__body {
        flex:1;
        overflow: auto;
    }
    .el-dialog__body{
        padding:5px 5px !important;
    }
</style>